package com.zty.errors;

import com.zty.interfaces.IResponse;
import lombok.Data;

import java.text.MessageFormat;

/**
 * @Author ZTY
 * @Description 定义业务异常类
 * @Date 0:24 2023-12-23
 **/

@Data
public class BusinessException extends RuntimeException implements IResponse {

    private final int code;
    private final String message;
    private final IResponse response;

    public BusinessException(IResponse response){
        this("",response,null);
    }

    public BusinessException(String message,IResponse response){
        this(message,response,null);
    }

    public BusinessException(String message,IResponse response,Object[] args){
        super(message == null ? response.getMsg() : message);
        this.code = response.getCode();
        this.response = response;
        this.message = MessageFormat.format(response.getMsg(), args);
    }

    @Override
    public int getCode() {
        return code;
    }

    @Override
    public String getMsg() {
        return message;
    }
}